import SysLoginLogModel from "../../dao/model/system/SysLoginLogModel";
import SysLoginLogEntity, { ISysLoginLogModel } from "../../entities/system/SysLoginLogEntity";
import { IPageResponse } from "../../types/CommonType";
// import { ValidationError } from "../../utils/errors";

export class LoginLogService {
  /**
   * 添加数据
   */
  public static async add(data: SysLoginLogEntity): Promise<null> {
    await SysLoginLogModel.create<ISysLoginLogModel>(data);
    return null;
  }

  /**
   * 分页获取数据
   */
  public static async findByPage(current: number, size: number, where?): Promise<IPageResponse> {
    let data = await SysLoginLogModel.findAndCountAll<ISysLoginLogModel>({
      offset: size * (current - 1),
      limit: size,
      where,
    });

    const result = {
      total: data.count,
      list: data.rows.map((item) => item.dataValues),
    };
    return result;
  }

  /**
   * 通过id获取数据
   */
  public static async findById(id: string) {
    const typeData = await SysLoginLogModel.findByPk<ISysLoginLogModel>(id, {});

    return typeData;
  }

  /**
   * 修改数据
   */
  public static async update(data: SysLoginLogEntity) {
    const result = SysLoginLogModel.update<ISysLoginLogModel>(data, {
      where: {
        id: data.id,
      },
    });

    // 将修改后的数据查出来
    await SysLoginLogModel.findOne<ISysLoginLogModel>({
      where: {
        id: data.id,
      },
    });

    return result;
  }

  /**
   * 删除数据
   */
  public static async delete(id: string) {
    return SysLoginLogModel.destroy({
      where: {
        id,
      },
    });
  }
}
